//
//  ScoreAnalysisView.m
//  Learn
//
//  Created by  on 2024/4/11.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ScoreAnalysisView.h"
#import "BarEntry.h"

@implementation ScoreAnalysisView

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 绘制图表名称
    NSString *chartTitle = @"人数";
    NSDictionary *titleAttributes = @{
        NSFontAttributeName: [UIFont boldSystemFontOfSize:12],
        NSForegroundColorAttributeName: rgba(142, 142, 142, 1)
    };
    CGSize titleSize = [chartTitle sizeWithAttributes:titleAttributes];
    [chartTitle drawAtPoint:CGPointMake(0, 0) withAttributes:titleAttributes];
    
    // 绘制Y轴轴线
    CGContextMoveToPoint(context, 30, CGRectGetHeight(self.bounds) - 45);
    CGContextAddLineToPoint(context, 30, 12);
    CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextStrokePath(context);
    
    // 绘制X轴轴线
    CGContextMoveToPoint(context, 30, CGRectGetHeight(self.bounds) - 45);
    CGContextAddLineToPoint(context, CGRectGetWidth(self.bounds) - 30, CGRectGetHeight(self.bounds) - 45);
    CGContextSetStrokeColorWithColor(context, rgba(229, 229, 229, 1).CGColor);
    CGContextStrokePath(context);
    
    // 计算Y轴分段数值间隔和分段高度
    CGFloat segmentHeight = (CGRectGetHeight(self.bounds) - 40) / 6;
    CGFloat segmentValue = [self maxValue]*1.3 / 6;
    
    // 绘制Y轴分段线和数值
    for (NSUInteger i = 0; i <= 6; i++) {
        // 设置虚线样式
//                CGFloat lengths[] = {5, 5};
//                CGContextSetLineDash(context, 0, lengths, 2);
                
                // 绘制分段线
                CGContextMoveToPoint(context, 30, CGRectGetHeight(self.bounds) - 45 - i * segmentHeight);
                CGContextAddLineToPoint(context, CGRectGetWidth(self.bounds) - 30, CGRectGetHeight(self.bounds) - 45 - i * segmentHeight);
                CGContextSetStrokeColorWithColor(context, rgba(229, 229, 229, 1).CGColor);
                CGContextStrokePath(context);
                
        // 绘制数值
                NSString *valueString = [NSString stringWithFormat:@"%.0f", segmentValue * i];
                NSDictionary *valueAttributes = @{
                    NSFontAttributeName: [UIFont systemFontOfSize:10],
                    NSForegroundColorAttributeName: rgba(142, 142, 142, 1)
                };
                CGSize valueSize = [valueString sizeWithAttributes:valueAttributes];
                [valueString drawAtPoint:CGPointMake(30 - valueSize.width - 5, CGRectGetHeight(self.bounds) - 45 - i * segmentHeight - valueSize.height / 2) withAttributes:valueAttributes];
    }
    
    // 绘制X轴标签
    NSDictionary *xLabelAttributes = @{
        NSFontAttributeName: [UIFont systemFontOfSize:10],
        NSForegroundColorAttributeName: rgba(142, 142, 142, 1)
    };
    for (NSUInteger index = 0; index < self.dataEntries.count; index++) {
        BarEntry *entry = self.dataEntries[index];
        CGSize xLabelSize = [entry.label sizeWithAttributes:xLabelAttributes];
        CGRect xLabelRect = CGRectMake(20 + index * (CGRectGetWidth(self.bounds) - 60) / self.dataEntries.count + (CGRectGetWidth(self.bounds) - 60) / self.dataEntries.count / 2 - xLabelSize.width / 2,
                                        CGRectGetHeight(self.bounds) - 30,
                                        (CGRectGetWidth(self.bounds) - 40) / self.dataEntries.count,
                                        20);
        
        // 保存当前上下文状态
        CGContextSaveGState(context);
        
        // 进行文字旋转
        CGContextTranslateCTM(context, CGRectGetMidX(xLabelRect), CGRectGetMidY(xLabelRect));
        CGContextRotateCTM(context, -M_PI_4); // 旋转45度
        
        // 绘制X轴标签
        NSAttributedString *xLabelText = [[NSAttributedString alloc] initWithString:entry.label attributes:xLabelAttributes];
        [xLabelText drawInRect:CGRectMake(-xLabelSize.width / 2, -xLabelSize.height / 2, xLabelSize.width, xLabelSize.height)];
        
        // 恢复上下文状态
        CGContextRestoreGState(context);
    }
    
    // 绘制柱状图
    CGFloat barWidth = 12; // 柱状图宽度默认值
    for (NSUInteger index = 0; index < self.dataEntries.count; index++) {
        BarEntry *entry = self.dataEntries[index];
        CGRect barRect = CGRectMake(30 + index * (CGRectGetWidth(self.bounds) - 60) / self.dataEntries.count + (CGRectGetWidth(self.bounds) - 60) / self.dataEntries.count / 2 - barWidth / 2,
                                    CGRectGetHeight(self.bounds) - 45 - entry.value * (CGRectGetHeight(self.bounds) - 40) / [self maxValue] / 1.3,
                                    barWidth,
                                    entry.value * (CGRectGetHeight(self.bounds) - 40) / [self maxValue] / 1.3);
        CGContextSetFillColorWithColor(context, entry.color.CGColor);
        CGContextFillRect(context, barRect);
        
        // 绘制柱状图顶部数值
        NSString *valueString = [NSString stringWithFormat:@"%.0f", entry.value];
        NSDictionary *valueAttributes = @{
            NSFontAttributeName: [UIFont systemFontOfSize:12],
            NSForegroundColorAttributeName: rgba(12, 183, 108, 1)
        };
        CGSize valueSize = [valueString sizeWithAttributes:valueAttributes];
        [valueString drawAtPoint:CGPointMake(30 + index * (CGRectGetWidth(self.bounds) - 60) / self.dataEntries.count + (CGRectGetWidth(self.bounds) - 60) / self.dataEntries.count / 2 - valueSize.width / 2,
                                              CGRectGetHeight(self.bounds) - 45 - entry.value * (CGRectGetHeight(self.bounds) - 40) / [self maxValue]/1.3 - valueSize.height - 5)
                  withAttributes:valueAttributes];
    }
}

- (CGFloat)maxValue {
    CGFloat max = 0;
    for (BarEntry *entry in self.dataEntries) {
        if (entry.value > max) {
            max = entry.value;
        }
    }
    return max;
}

@end
